package servletAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;

import util.DomainUtils;
import util.FormArgumentsException;
import actionForm.UpdateContactValidationForm;
import domain.dao.interfaces.IDAOContact;
import domain.entities.interfaces.IContact;
import domain.entities.interfaces.IEntreprise;

public class UpdateContactAction extends Action {

	static int versionX = -1;

	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse) {
		final UpdateContactValidationForm lForm = (UpdateContactValidationForm) pForm;

		String selected_contact_version = lForm.getSelected_contact_version();
		String selected_contact = lForm.getSelected_contact();
		String first_name = lForm.getFirst_name();
		String last_name = lForm.getLast_name();
		String e_mail = lForm.getE_mail();
		String street = lForm.getStreet();
		String city = lForm.getCity();
		String zip = lForm.getZip();
		String country = lForm.getCountry();
		String mobile = lForm.getMobile();
		String house = lForm.getHouse();
		String work = lForm.getWork();
		String num_siret = lForm.getNum_siret();
		IContact c = null; 

		ApplicationContext context = util.SpringUtils.getApplicationContext();

		try {

			IDAOContact daoContact = (IDAOContact) context
					.getBean("beanDAOContact");



			c = daoContact.getById(Integer.parseInt(selected_contact));

			checks(pRequest, c, selected_contact_version);



			if (num_siret == null || num_siret.isEmpty()) {

				daoContact.updateContact(
						c, first_name, last_name, e_mail, 
						street, city, zip, country, mobile,
						house, work);

			} else {

				daoContact.updateEnterprise(
						(IEntreprise)c, first_name, last_name, e_mail, street, 
						city, zip, country, mobile, house, work,
						Integer.parseInt(num_siret));
			}

			DomainUtils.displayInfoMessage(pRequest,
					DomainUtils.UPDATE_CONTACT_ALERT_CURRENT_COLOR,
					"Contact was update successfully.");

		} catch (Exception e) {

			try {
				IDAOContact daoContact = (IDAOContact) context.getBean("beanDAOContact");

				if ((e instanceof FormArgumentsException) == false) {
					pRequest.setAttribute(DomainUtils.ALERT_ERROR_STRING,
							"An error occur on the server.");
				}
				else if(e instanceof HibernateOptimisticLockingFailureException) {
					pRequest.setAttribute(
							DomainUtils.ALERT_ERROR_STRING, 
							"This contact group has just been modified. "
							+ "Please, before submit check modification on details informations.");

				}
				else if(daoContact.getById(Integer.parseInt(selected_contact)) == null) {
					pRequest.setAttribute(
							DomainUtils.ALERT_ERROR_STRING, 
							"This contact has been deleted.");
				}
			}
			catch(Exception e2) {
				pRequest.setAttribute(DomainUtils.ALERT_ERROR_STRING,
						"An error occur on the server.");
			}
		}

		
		DomainUtils.keepParametersContent(pRequest);

		return pMapping.findForward("stay");

	}

	public void checks(HttpServletRequest request, 
			IContact c, String selected_contact_num_version) throws FormArgumentsException {
		int version = Integer.parseInt(selected_contact_num_version);

		if(c.getVersion() != version) {
			request.setAttribute(
					DomainUtils.ALERT_ERROR_STRING, 
					"This contact group has just been modified. Please, before "
					+ "submit check modification on details informations.");

			throw new FormArgumentsException();
		}
	}
}
